package com.jt.www.biz.order_pc.service.impl;

import com.jt.www.biz.order_pc.service.TaxpayerInfoService;
import com.jt.www.dao.mapper.insured.TaxpayerInfoMapper;
import com.jt.www.model.insured.TaxpayerInfoEntity;
import com.jt.www.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

/**
 * @author lq
 * @create 2019-11-14 14:36
 */
@Service
public class TaxpayerInfoServiceImpl implements TaxpayerInfoService {

    @Autowired
    private TaxpayerInfoMapper taxpayerInfoMapper;


    @Override
    public Integer updateTaxpayerInfo(TaxpayerInfoEntity taxpayerInfoEntity) {

        if (taxpayerInfoEntity == null) {
            return -1;
        }

        int num = 0;
        String currentTime = DateUtils.getCurrentTime();

        // 判断是否都为空，都为空则不进行修改或新增
        if (StringUtils.isNotBlank(taxpayerInfoEntity.getTaxpayerAddress())
                || StringUtils.isNotBlank(taxpayerInfoEntity.getTaxpayerPhone())
                || StringUtils.isNotBlank(taxpayerInfoEntity.getTaxpayerBankName())
                || StringUtils.isNotBlank(taxpayerInfoEntity.getTaxpayerBankNo())) {
            Weekend<TaxpayerInfoEntity> weekend = Weekend.of(TaxpayerInfoEntity.class);
            WeekendCriteria<TaxpayerInfoEntity, Object> criteria = weekend.weekendCriteria();
            criteria.andEqualTo(TaxpayerInfoEntity::getUserCode, taxpayerInfoEntity.getUserCode());
            criteria.andEqualTo(TaxpayerInfoEntity::getType, taxpayerInfoEntity.getType());
            int count = taxpayerInfoMapper.selectCountByExample(weekend);

            // 已存在，则更新
            if (count > 0) {
                taxpayerInfoEntity.setUpdateTime(currentTime);
                taxpayerInfoEntity.setIsDeleted(false);
                num = taxpayerInfoMapper.updateByPrimaryKey(taxpayerInfoEntity);
            } else {// 不存在，则新增
                taxpayerInfoEntity.setCreateTime(currentTime);
                taxpayerInfoEntity.setIsDeleted(false);
                num = taxpayerInfoMapper.insertSelective(taxpayerInfoEntity);
            }
        }

        return num;
    }

    @Override
    public Integer deleteTaxpayerInfo(String id) {

        TaxpayerInfoEntity taxpayerInfoEntity = new TaxpayerInfoEntity();
        taxpayerInfoEntity.setId(Long.valueOf(id));
        taxpayerInfoEntity.setIsDeleted(true);
        taxpayerInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        int num = taxpayerInfoMapper.updateByPrimaryKeySelective(taxpayerInfoEntity);
        return num;
    }

    @Override
    public List<TaxpayerInfoEntity> searchTaxpayerInfoByUserCode(String userCode, String type) {
        TaxpayerInfoEntity t = new TaxpayerInfoEntity();
        t.setUserCode(userCode);
        t.setType(type);
        t.setIsDeleted(false);
        List<TaxpayerInfoEntity> infoList = taxpayerInfoMapper.select(t);
        return infoList;
    }
}
